package com.loser.csp.webapp.backcontroller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.loser.csp.dal.model.Admin;
import com.loser.csp.service.backmanager.AdminServices;

/**
 * 
 * @author alan
 *
 */
@Controller
public class AdminLoginController {
    @Autowired
    private AdminServices adminServices;

    @RequestMapping(value = "/backlogin.html", method = RequestMethod.GET)
    public ModelAndView loadLogin(Model mode) {
        System.out.println(2333);
        ModelAndView view=new ModelAndView();        
        view.setViewName("login");
        return view;
    }

    /**
     * 
     * @param modelMap
     * @param loginName
     * @param password
     * @return
     */
    @RequestMapping(value = "/index.html", method = RequestMethod.POST)
    public ModelAndView login(ModelMap modelMap,HttpServletRequest request, String loginName, String password) {
        Admin admin = adminServices.backLogin(loginName.trim(), password.trim());
        if (admin != null) {
            if (StringUtils.equals(loginName, admin.getLoginid())
                && StringUtils.equals(password, admin.getPassword())) {   
            	 System.out.println("admin=="+admin);
                request.getSession().setAttribute("loginName", admin.getName());
                System.out.println("mode=="+request.getSession().getAttribute("loginName"));
                return new ModelAndView("index");
            }
        }
        return new ModelAndView("error");
    }
    @RequestMapping(value="/test.html",method=RequestMethod.GET)
    public String test(ModelMap map){
    	map.addAttribute("loginName1", "三风");
    	return "test1";
    }

}
